﻿/**
 * 滚动条大小不变
 * 
 */
package com.yy.yyui.scroll
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	/**
	 * 
	 * @author YY
	 * 
	 */
	public class ScrollPlus extends Sprite
	{
		/**
		 * 被遮罩对对象
		 * 遮罩区域
		 * 滚动条背景
		 * 滚动条
		 * 向上按钮
		 * 向下按钮
		 */
		 
		private var _maskee:DisplayObject;
		private var _displayArea:Object;
		private var _bg:Sprite;
		private var _scrollBar:Sprite;
		private var _upBtn:*;
		private var _downBtn:*;
		private var _displaySprite:Sprite;
		public function ScrollPlus(	display:DisplayObject,
								   	displayArea:*,
									bg:Sprite,
									scrollBar:Sprite,
									upBtn:*=null,
									downBtn:*=null			   	
								   	){
				this._maskee = display;
				this._displayArea = displayArea;
				this._bg = bg;
				this._scrollBar = scrollBar;
				this._scrollBar.buttonMode = true;
				this._upBtn = upBtn;
				this._downBtn = downBtn;
			initFunc();
		}
		private function  initFunc():void {
			this.setDisplayArea(this._displayArea);
			if(this._displayArea.height > this._maskee.height) {
				this._scrollBar.visible = false;
				return;
			}
			update();
			addListener();
		}
		private var _rect:Rectangle = new Rectangle();
		private function addListener():void {
			this._scrollBar.addEventListener(MouseEvent.MOUSE_DOWN, downFunc);
			if(this._upBtn) {
				this._upBtn.addEventListener(MouseEvent.MOUSE_DOWN, downUpBtnFunc);
			}
			if(this._downBtn) {
				this._downBtn.addEventListener(MouseEvent.MOUSE_DOWN, downDownBtnFunc);
			}
		}
		private function downDownBtnFunc(e:MouseEvent):void {
			if(this._scrollBar.visible) {
				var temp:int = this._scrollBar.height + this._scrollBar.y + 3;
				if(temp >= this._bg.height) {
					this._scrollBar.y = this._bg.height - this._scrollBar.height;
				}else {
					this._scrollBar.y += 3;
				}
				this.move();
			}
		}
		private function downUpBtnFunc(e:MouseEvent):void {
			if(this._scrollBar.visible) {
				var temp:int = this._scrollBar.y - 3;
				if(temp <=0 ) {
					this._scrollBar.y = 0;
				}else {
					this._scrollBar.y = temp;
				}
				this.move();
			}
		}
		private var _moveTimer:Timer = new Timer(100);
		private function upFunc(e:MouseEvent):void {
			this._scrollBar.stopDrag();
			if(this._maskee.stage) {
				this._maskee.stage.removeEventListener(MouseEvent.MOUSE_UP, upFunc);
			}
			this._moveTimer.removeEventListener(TimerEvent.TIMER, moveTimerFunc);
		}
		private function downFunc(e:MouseEvent):void {
			if(this._maskee.stage) {
				this._maskee.stage.addEventListener(MouseEvent.MOUSE_UP, upFunc);
			}
			this._scrollBar.startDrag(false, this._rect);
			this._moveTimer.addEventListener(TimerEvent.TIMER, moveTimerFunc);
			this._moveTimer.start();
		}
		private function moveTimerFunc(e:TimerEvent):void{
			move();
		}
		public function update():void {
			this._rect.height = this._bg.height - this._scrollBar.height;
			this._rect.width = 0;
			
			if(this._displayArea.height<this._maskee.height) {
				this._scrollBar.visible = true;
				if(!this._scrollBar.hasEventListener(MouseEvent.MOUSE_DOWN)) {
					this._scrollBar.addEventListener(MouseEvent.MOUSE_DOWN, downFunc);
				}
				if(this._upBtn && !this._upBtn.hasEventListener(MouseEvent.MOUSE_DOWN)) {
					this._upBtn.addEventListener(MouseEvent.MOUSE_DOWN, downUpBtnFunc);
				}
				if(this._downBtn && !this._downBtn.hasEventListener(MouseEvent.MOUSE_DOWN)) {
					this._downBtn.addEventListener(MouseEvent.MOUSE_DOWN, downDownBtnFunc);
				}
			}else {
				this._scrollBar.visible = false;
			}
		}
		public function move():void {
			this._maskee.y = this._displaySprite.y - this._scrollBar.y/this._rect.height*(this._maskee.height-this._displayArea.height);
		}
		public function setDisplayArea(area:*):void {
			if(this._displaySprite) {
				Sprite(this._maskee.parent).removeChild(this._displaySprite);
			}
			this._displayArea = area;
			this._displaySprite = new Sprite();
			this._displaySprite.x = this._maskee.x;
			this._displaySprite.y = this._maskee.y;
			this._displaySprite.graphics.beginFill(0xffff00, 1);
			this._displaySprite.graphics.drawRect(0,0, this._displayArea.width, this._displayArea.height);
			Sprite(this._maskee.parent).addChild(this._displaySprite);
			this._maskee.mask = this._displaySprite;
		}
		public function toBotton():void {
			this._scrollBar.y = this._bg.height - this._scrollBar.height;
			move();
		}
		public function toTop():void {
			this._scrollBar.y = 0;
			move();
		}
	}
}





